<template>
	<view class="content">
		<u-navbar class="head-index" :fixed="false" :safeAreaInsetTop="true" bgColor="#51d0d0" titleStyle="color:#fff"
			:autoBack="false" leftIcon=" " title="应用授权信息">
			<view class="u-nav-slot" slot="right">
				<m-capsule :closeHandler="ClosePage"></m-capsule>
			</view>
		</u-navbar>

		<view class="MainBox">
			<view class="CardBox">
				<view class="TitleText">
					<text>APP授权信息</text>
				</view>

				<view class="EmpowerBox">
					<view v-if="!IsTrue" @click="ToggleBtn" class="TextBtn">
						<text>申请授权</text>
					</view>

					<view v-else class="TextBtn">
						<text>已授权</text>
					</view>
				</view>

				<view class="CardMain">

					<view class="Top_isTrue_Text">
						<uni-icons :type="!IsTrue ?  'locked' : 'calendar'" :color="!IsTrue ?  '#ff0000' : '#3ed180'"
							size="60"></uni-icons>

						<view>
							<text v-if="!IsTrue" class="isTrue_Text">本机未授权，请先前往申请授权</text>
							<text v-else class="isTrue_Text">本机已授权，允许使用软件</text>
							<text class="Text">软件授权</text>
						</view>
					</view>

					<view class="Top_Version_Box">
						<text class="TitleText">当前版本</text>
						<text class="Text">{{version}}</text>
					</view>

					<view class="Top_Status_Box">
						<text class="TitleText">授权内容</text>

						<view v-if="!IsTrue" class="Status_Box">
							<!-- <uni-icons color="#ff0000" custom-prefix="iconfont" type="icon-notquanshouquan"
								size="20"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#ff0000"
								:size="20">{{$utils.toUniCode('&#xe70b;')}}</uni-icons>
							<text>当前版本未授权</text>
						</view>

						<view v-else class="Status_Box">
							<!-- <uni-icons type="checkmarkempty" color="#3ed180" size="20"></uni-icons> -->
							<!-- <uni-icons color="#3ed180" custom-prefix="iconfont" type="icon-shouquan1"
								size="20"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#3ed180"
								:size="20">{{$utils.toUniCode('&#xe602;')}}</uni-icons>
							<text>当前版本已授权</text>
						</view>
					</view>

					<view class="Top_MachineCode_Box" @click="$utils.TextCopyFun(OnlyIdentity)">
						<text class="TitleText">当前设备机器码</text>

						<view class="MachineCode_Box">
							<!-- <uni-icons color="#333" custom-prefix="iconfont" type="icon-jiqima" size="28"></uni-icons> -->

							<uni-icons fontFamily="CustomFontPlatform" color="#333"
								:size="28">{{$utils.toUniCode('&#xe84d;')}}</uni-icons>

							<text style="margin-left: 10rpx;">{{OnlyIdentity}}</text>


							<uni-icons v-if="OnlyIdentity" fontFamily="CustomFontPlatform" color="#0647c1"
								style="margin-left: 4rpx;" size="16">{{$utils.toUniCode('&#xe862;')}}</uni-icons>
						</view>
					</view>

				</view>

			</view>


			<view v-show="IsTrue" class="CardBox">
				<view class="TitleText">
					<text>授权时间</text>
				</view>

				<view class="CardMain">
					<view style="font-weight: 600;">
						<text>{{CreateTime}} 至 {{expireDate}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				OnlyIdentity: '', // 机器码
				IsTrue: false, // 是否授权
				CreateTime: null, // 授权时间
				expireDate: null, // 授权期限
				version: null, // 版本号
			}
		},
		onLoad() {
			var that = this;

			that.version = plus.runtime.version;
			console.log(this.$store.state.systemConfig.sysDeviceInfo.deviceId);
			if (this.$store.state.systemConfig.sysDeviceInfo.deviceId) {
				that.OnlyIdentity = this.$store.state.systemConfig.sysDeviceInfo.deviceId;
			};

			// 赋值授权信息
			if (!this.$utils.isEmpty(this.$store.state.appConfigInfo.Authorization_Info)) {
				let data = this.$store.state.appConfigInfo.Authorization_Info;
				that.IsTrue = data.Authorization;
				that.CreateTime = data.CreateTime.split('T')[0];
				that.expireDate = data.EffectiveDate.split('T')[0];
			};
		},
		methods: {
			ClosePage() {

				let routes = getCurrentPages(); //当前页面栈
				console.log(routes.length);
				if (routes.length == 1) { // 未授权状态
					uni.setStorageSync('Automatic_Login', false);
					uni.redirectTo({
						url: "/pages/login/login"
					});
				} else {
					uni.navigateBack();
				}
			},
			ToggleBtn() { // 点击切换授权
				var that = this;
				that.$utils.showLoading();

				setTimeout(() => {
					that.$utils.hideLoading();

					if (!that.IsTrue) { // 未授权状态，前往授权
						that.apply()
					}


					// that.IsTrue = !that.IsTrue
				}, 1000)
			},
			apply() {
				var that = this
				let obj = {
					// cmdname: 'applylicense',
					machineId: that.OnlyIdentity,
					machineType: 2
				}
				that.Request.request('applylicense', obj, function(res) {
					if (res.status == 200) {

						if (res.data.ds[0].status == 200) {
							that.IsTrue = true

							that.searchApply()

							uni.showToast({
								title: '授权成功',
							})
							setTimeout(() => {
								uni.hideToast();
								// 重启设备
								plus.runtime.restart();
							}, 800);

						} else if (res.data.ds[0].status == 500) {
							that.IsTrue = false
							uni.showToast({
								title: res.data.ds[0].msg,
								icon: "none",
							});
							setTimeout(() => {
								uni.hideToast();
							}, 1000);

						} else {
							that.IsTrue = false

							uni.showToast({
								title: '授权失败',
								icon: "none",
							})
							setTimeout(() => {
								uni.hideToast();
							}, 800);

						}
					}

				}, function(err) {
					console.log(JSON.stringify(err))
				}, that.Request.IPAgreement() + that.Request.serverIP() + '/Frame/SqlData', 1)
			},
			searchApply() { // 产看本机是否授权
				var that = this
				let obj = {
					// cmdname: 'searchapply',
					machineId: that.OnlyIdentity,
				}
				that.Request.request('searchapply', obj, function(res) {
					if (res.status == 200) {
						if (res.data.ds.length) {
							// 已授权
							that.$store.state.Authorization = true

						} else {}
					}

				}, function(err) {
					console.log(JSON.stringify(err))
				}, that.Request.IPAgreement() + that.Request.serverIP() + '/Frame/SqlData', 1)
			},
		}
	}
</script>

<style lang="scss" scoped>
	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	.content {
		// background-color: #f8f8f8;
		background-color: #f5f6fa;
		height: 100vh;
		display: flex;
		flex-direction: column;

		.MainBox {
			flex: 1;
			padding: 0px 15px;

			.CardBox {
				position: relative;
				padding: 15px 10px;
				box-shadow: 1px 1px 13px -10px #999;
				min-height: 60px;
				background-color: #f8f8f8;
				border-radius: 12px;
				margin-top: 10px;

				// 标题样式
				.TitleText {
					display: block;
					font-size: 16px;
					color: #333;
					font-weight: 600;
				}

				// 右上角申请按钮
				.EmpowerBox {
					position: absolute;
					right: 10rpx;
					top: 10rpx;

					.TextBtn:first-child {
						width: fit-content;
						border-radius: 99999px;
						padding: 6px 15px;
						background-color: #51d0d0;
						color: #fff;
					}

					.TextBtn:last-child {
						width: fit-content;
						border-radius: 99999px;
						padding: 6px 15px;
						background-color: #fff;
						color: #51d0d0;
						border: 1px solid #51d0d0;
					}
				}

				.CardMain {
					margin-top: 15px;
					font-size: 14px;

					// 是否授权 标题文字
					.Top_isTrue_Text {
						display: flex;
						align-items: center;

						.isTrue_Text {
							display: block;
							font-size: 14px;
							color: #999;

							.Text {
								display: block;
								font-size: 18px;
								color: #333;
								font-weight: 600;
							}
						}
					}

					// 版本
					.Top_Version_Box {
						margin-top: 12px;

						.Text {
							display: block;
							font-size: 14px;
							color: #333;
							margin-top: 8px;
						}
					}

					// 授权内容状态
					.Top_Status_Box {
						margin-top: 12px;

						.Status_Box {
							display: flex;
							align-items: center;
							margin-top: 8px;
							font-size: 14px;
						}
					}

					// 机器码
					.Top_MachineCode_Box {
						margin-top: 12px;

						.MachineCode_Box {
							display: flex;
							align-items: center;
							margin-top: 8px;
							color: #0647c1;
							font-size: 14px;
						}
					}

				}
			}


		}
	}
</style>